<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    // options = {
    //   url: 'xxxx',
    //   method: 'GET',
    //   data: {},
    //   dataType: 'json',
    //   success: () => {}
    // }
    function ajax(options = {}) {
      const {method = 'GET', url, data, success, error} = options
      // XMLHttpRequest
      const xhr = new XMLHttpRequest()
      // 连接
      xhr.open(method, url, true)
      // 设置请求头
      xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
      // 发送请求
      xhr.send(data)
      // 处理响应
      xhr.onreadystatechange = function() {
        if (xhr.readyState === 4) {
          if (xhr.status === 200) {
            const data = JSON.parse(xhr.responseText)
            success && success(data)
          } else {
            error && error(xhr.statusText)
          }
        }
      }
    }

    function promiseAjax(options) {
      return new Promise((resolve, reject) => {
        ajax({
          ...options,
          success: data => resolve(data),
          error: err => reject(err)
        })
      })
    }

    promiseAjax({
      url: 'http://localhost:9527/api/v1/users?page=2&pageSize=2',
      type: 'GET'
    })
      .then(console.log)
      .catch(console.error)
  </script>
</body>
</html>